package com.freeter.modules.article.controller;

import java.awt.*;
import java.util.Arrays;
import java.util.List;

import com.freeter.common.validator.ValidatorUtils;
import org.apache.commons.lang.StringEscapeUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.freeter.common.utils.JQPageInfo;
import com.freeter.common.mpextend.parser.ParseWrapper;
import com.freeter.common.utils.PageInfo;


import com.freeter.modules.article.entity.CmsArticleEntity;
import com.freeter.modules.article.entity.view.CmsArticleView;
import com.freeter.modules.article.entity.model.CmsArticleModel;

import com.freeter.modules.article.service.CmsArticleService;
import com.freeter.common.utils.PageUtils;
import com.freeter.common.utils.R;
import com.freeter.common.utils.MPUtil;



/**
 * CMS文章表
 * 后端接口
 * @author xuchen
 * @email 171998110@qq.com
 * @date 2019-08-16 14:50:32
 */
@RestController
@RequestMapping("article/cmsarticle")
@SuppressWarnings({"unchecked","rawtypes"})
public class CmsArticleController {
    @Autowired
    private CmsArticleService cmsArticleService;

    /**
     * 列表
     */
    @RequestMapping("/page")
    @RequiresPermissions("article:cmsarticle:list")
    public PageUtils page(JQPageInfo jqPageInfo,CmsArticleModel cmsArticle){
        EntityWrapper< CmsArticleEntity> ew = ParseWrapper.parseWrapper(cmsArticle);
     	PageInfo pageInfo = new PageInfo(jqPageInfo);
     	PageUtils page = cmsArticleService.queryPage(pageInfo, ew);

        return page;
        
    }

	/**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("article:cmsarticle:list")
    public R list( CmsArticleModel cmsArticle){
       	EntityWrapper< CmsArticleEntity> ew = ParseWrapper.parseWrapper(cmsArticle);
        return R.ok().put("data",  cmsArticleService.selectListView(ew));
    }

	 /**
     * 查询
     */
    @RequestMapping("/query")
    @RequiresPermissions("article:cmsarticle:info")
    public R query(CmsArticleEntity cmsArticle){
        EntityWrapper< CmsArticleEntity> ew = ParseWrapper.parseWrapper(cmsArticle);
		CmsArticleView  cmsArticleView =  cmsArticleService.selectView(ew);
		return R.ok("查询CMS文章表成功").put("data",  cmsArticleView);
    }
	
    /**
     * 信息
     */
    @RequestMapping("/info/{articleId}")
    @RequiresPermissions("article:cmsarticle:info")
    public R info(@PathVariable("articleId") Integer articleId){
        CmsArticleEntity cmsArticle = cmsArticleService.selectById(articleId);

        return R.ok().put("cmsArticle", cmsArticle);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("article:cmsarticle:save")
    public R save(@RequestBody CmsArticleEntity cmsArticle,String content){
        cmsArticle.setContent(content);
    	ValidatorUtils.validateEntity(cmsArticle);
        cmsArticleService.insert(cmsArticle);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping(value = "/update",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @RequiresPermissions("article:cmsarticle:update")
    public R update(@RequestBody CmsArticleEntity cmsArticle,String content){
        cmsArticle.setContent(content);
        ValidatorUtils.validateEntity(cmsArticle);
        cmsArticleService.updateAllColumnById(cmsArticle);//全部更新

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("article:cmsarticle:delete")
    public R delete(@RequestBody Integer[] articleIds){
        cmsArticleService.deleteBatchIds(Arrays.asList(articleIds));

        return R.ok();
    }

}
